#!/usr/bin/perl.exe -w


if(@ARGV!=4) {
	die "Please 4 file name as input Log File eg:DWH_REQ_LOG.csv DWH_REQCON_LOG.csv DWH_RES_LOG.csv TAT_RESULT.log\n"
}

$fileReq=$ARGV[0];
open MYFILE_REQ, "$fileReq" or die "$fileReq: $!\n";

$fileReqCon=$ARGV[1];
open MYFILE_REQ_CON, "$fileReqCon" or die "$fileReqCon: $!\n";


$fileRes=$ARGV[2];
open MYFILE_RES, "$fileRes" or die "$fileRes: $!\n";

$saveResult=$ARGV[3];
open (OUTPUT,">$saveResult")||die "Can't open file $saveResult:$!\n" ;

my $reqLines="";
my $reqConLines="";
my $resLines="";
my %ouputTATHash;
my $outputLine;

my @eclipseTimeArray=qw{0 0 0 0 0 0 0};
my @eclipseTimeArrayDJ=qw{0 0 0 0 0 0 0};
my @eclipseTimeArrayPI=qw{0 0 0 0 0 0 0};
my @eclipseTimeArrayTI=qw{0 0 0 0 0 0 0};
my @eclipseTimeArrayCP=qw{0 0 0 0 0 0 0};
my @eclipseTimeArrayOP=qw{0 0 0 0 0 0 0};
my @eclipseTimeArrayGLS=qw{0 0 0 0 0 0 0};

while ($reqLines=<MYFILE_REQ>){
	chomp $reqLines;
	my @fields=split/,/,$reqLines;
	if(@fields){
		my @eachLineArray=$fields[1];
		$ouputTATHash{$fields[5]}=[@eachLineArray];
	}
}

while ($resLines=<MYFILE_RES>){
	chomp $resLines;
	my @fields=split/,/,$resLines;
	if(exists $ouputTATHash{$fields[5]}){
		@outputLineArray=@{$ouputTATHash{$fields[5]}};
		$outputLineArray[1]=$fields[1];
		$ouputTATHash{$fields[5]}=[@outputLineArray];
	}
}

while ($reqConLines=<MYFILE_REQ_CON>){
	chomp $reqConLines;
	my @fields=split/,/,$reqConLines;
	if(exists $ouputTATHash{$fields[6]}){
		@outputLineArray=@{$ouputTATHash{$fields[6]}};
		$outputLineArray[2]=$fields[8];
	    $ouputTATHash{$fields[6]}=[@outputLineArray];
	}
}

foreach my $key ( keys %ouputTATHash )
{
    my $value = $ouputTATHash{$key};
    
	$outputLine=$key;
	
	my $TATResult=-1;	
	
	$outputLine.="\t";
	
	if(defined($$value[0])){
		$outputLine.=$$value[0];
	}
    $outputLine.="\t";
	if(defined($$value[1])){
		$outputLine.=$$value[1];
	}
	$outputLine.="\t";
	
	if(defined($$value[0])&&defined($$value[1])){
	    $TATResult=&TAT($$value[0],$$value[1]);
		$outputLine.=$TATResult;
	}else{
		$outputLine.="#N/A";
	}

	$outputLine.="\t";
	
	if(defined($$value[2])){
		$outputLine.=$$value[2];
	}else{
		$outputLine.="\t";
	}
	
	if(defined($$value[2])){
		if($TATResult<=10.0&&$TATResult>=0.0){
			$eclipseTimeArray[0]=$eclipseTimeArray[0]+1;
			if($$value[2] eq "DJ"){
				$eclipseTimeArrayDJ[0]=$eclipseTimeArrayDJ[0]+1;
			}elsif($$value[2] eq "PI"){
				$eclipseTimeArrayPI[0]=$eclipseTimeArrayPI[0]+1;
			}elsif($$value[2] eq "TI"){
				$eclipseTimeArrayTI[0]=$eclipseTimeArrayTI[0]+1;
			}elsif($$value[2] eq "CP"){
				$eclipseTimeArrayCP[0]=$eclipseTimeArrayCP[0]+1;
			}elsif($$value[2] eq "OP"){
				$eclipseTimeArrayOP[0]=$eclipseTimeArrayOP[0]+1;
			}elsif($$value[2] eq "GLS"){
				$eclipseTimeArrayGLS[0]=$eclipseTimeArrayGLS[0]+1;
			}
		}elsif($TATResult<=20.0&&$TATResult>10.0){
			$eclipseTimeArray[1]=$eclipseTimeArray[1]+1;
			if($$value[2] eq "DJ"){
				$eclipseTimeArrayDJ[1]=$eclipseTimeArrayDJ[1]+1;
			}elsif($$value[2] eq "PI"){
				$eclipseTimeArrayPI[1]=$eclipseTimeArrayPI[1]+1;
			}elsif($$value[2] eq "TI"){
				$eclipseTimeArrayTI[1]=$eclipseTimeArrayTI[1]+1;
			}elsif($$value[2] eq "CP"){
				$eclipseTimeArrayCP[1]=$eclipseTimeArrayCP[1]+1;
			}elsif($$value[2] eq "OP"){
				$eclipseTimeArrayOP[1]=$eclipseTimeArrayOP[1]+1;
			}elsif($$value[2] eq "GLS"){
				$eclipseTimeArrayGLS[1]=$eclipseTimeArrayGLS[1]+1;
			}
		}elsif($TATResult<=30.0&&$TATResult>20.0){
			$eclipseTimeArray[2]=$eclipseTimeArray[2]+1;
			if($$value[2] eq "DJ"){
				$eclipseTimeArrayDJ[2]=$eclipseTimeArrayDJ[2]+1;
			}elsif($$value[2] eq "PI"){
				$eclipseTimeArrayPI[2]=$eclipseTimeArrayPI[2]+1;
			}elsif($$value[2] eq "TI"){
				$eclipseTimeArrayTI[2]=$eclipseTimeArrayTI[2]+1;
			}elsif($$value[2] eq "CP"){
				$eclipseTimeArrayCP[2]=$eclipseTimeArrayCP[2]+1;
			}elsif($$value[2] eq "OP"){
				$eclipseTimeArrayOP[2]=$eclipseTimeArrayOP[2]+1;
			}elsif($$value[2] eq "GLS"){
				$eclipseTimeArrayGLS[2]=$eclipseTimeArrayGLS[2]+1;
			}		
		}elsif($TATResult<=40.0&&$TATResult>30.0){
			$eclipseTimeArray[3]=$eclipseTimeArray[3]+1;
			if($$value[2] eq "DJ"){
				$eclipseTimeArrayDJ[3]=$eclipseTimeArrayDJ[3]+1;
			}elsif($$value[2] eq "PI"){
				$eclipseTimeArrayPI[3]=$eclipseTimeArrayPI[3]+1;
			}elsif($$value[2] eq "TI"){
				$eclipseTimeArrayTI[3]=$eclipseTimeArrayTI[3]+1;
			}elsif($$value[2] eq "CP"){
				$eclipseTimeArrayCP[3]=$eclipseTimeArrayCP[3]+1;
			}elsif($$value[2] eq "OP"){
				$eclipseTimeArrayOP[3]=$eclipseTimeArrayOP[3]+1;
			}elsif($$value[2] eq "GLS"){
				$eclipseTimeArrayGLS[3]=$eclipseTimeArrayGLS[3]+1;
			}		
		}elsif($TATResult<=50.0&&$TATResult>40.0){
			$eclipseTimeArray[4]=$eclipseTimeArray[4]+1;
			if($$value[2] eq "DJ"){
				$eclipseTimeArrayDJ[4]=$eclipseTimeArrayDJ[4]+1;
			}elsif($$value[2] eq "PI"){
				$eclipseTimeArrayPI[4]=$eclipseTimeArrayPI[4]+1;
			}elsif($$value[2] eq "TI"){
				$eclipseTimeArrayTI[4]=$eclipseTimeArrayTI[4]+1;
			}elsif($$value[2] eq "CP"){
				$eclipseTimeArrayCP[4]=$eclipseTimeArrayCP[4]+1;
			}elsif($$value[2] eq "OP"){
				$eclipseTimeArrayOP[4]=$eclipseTimeArrayOP[4]+1;
			}elsif($$value[2] eq "GLS"){
				$eclipseTimeArrayGLS[4]=$eclipseTimeArrayGLS[4]+1;
			}		
		}elsif($TATResult>50.0){
			$eclipseTimeArray[5]=$eclipseTimeArray[5]+1;
			if($$value[2] eq "DJ"){
				$eclipseTimeArrayDJ[5]=$eclipseTimeArrayDJ[5]+1;
			}elsif($$value[2] eq "PI"){
				$eclipseTimeArrayPI[5]=$eclipseTimeArrayPI[5]+1;
			}elsif($$value[2] eq "TI"){
				$eclipseTimeArrayTI[5]=$eclipseTimeArrayTI[5]+1;
			}elsif($$value[2] eq "CP"){
				$eclipseTimeArrayCP[5]=$eclipseTimeArrayCP[5]+1;
			}elsif($$value[2] eq "OP"){
				$eclipseTimeArrayOP[5]=$eclipseTimeArrayOP[5]+1;
			}elsif($$value[2] eq "GLS"){
				$eclipseTimeArrayGLS[5]=$eclipseTimeArrayGLS[5]+1;
			}		
		}else{
			$eclipseTimeArray[6]=$eclipseTimeArray[6]+1;
			if($$value[2] eq "DJ"){
				$eclipseTimeArrayDJ[6]=$eclipseTimeArrayDJ[6]+1;
			}elsif($$value[2] eq "PI"){
				$eclipseTimeArrayPI[6]=$eclipseTimeArrayPI[6]+1;
			}elsif($$value[2] eq "TI"){
				$eclipseTimeArrayTI[6]=$eclipseTimeArrayTI[6]+1;
			}elsif($$value[2] eq "CP"){
				$eclipseTimeArrayCP[6]=$eclipseTimeArrayCP[6]+1;
			}elsif($$value[2] eq "OP"){
				$eclipseTimeArrayOP[6]=$eclipseTimeArrayOP[6]+1;
			}elsif($$value[2] eq "GLS"){
				$eclipseTimeArrayGLS[6]=$eclipseTimeArrayGLS[6]+1;
			}		
		}			
	}

	
	$outputLine.="\n";
	
	print OUTPUT $outputLine;
}


my $counter=0;

print OUTPUT "TOTAL\tDJ\tPI\tTI\tCP\tOP\tGLS\n";
while ($counter<=6){
	$outputLine=$eclipseTimeArray[$counter]."\t".$eclipseTimeArrayDJ[$counter]."\t";
	$outputLine.=$eclipseTimeArrayPI[$counter]."\t".$eclipseTimeArrayTI[$counter]."\t";
	$outputLine.=$eclipseTimeArrayCP[$counter]."\t".$eclipseTimeArrayOP[$counter]."\t";
	$outputLine.=$eclipseTimeArrayGLS[$counter]."\n";
	$counter=$counter+1;
	print OUTPUT $outputLine;
}

sub TAT{
	my ($startDatetime,$endDatetime);
	($startDatetime,$endDatetime)=@_;
	my ($yearStart,$monStart,$mdayStart,$hourStart,$minStart,$secStart);
	my ($yearEnd,$monEnd,$mdayEnd,$hourEnd,$minEnd,$secEnd);
    ($yearStart,$monStart,$mdayStart,$hourStart,$minStart,$secStart)=($startDatetime=~/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{5})$/);
    ($yearEnd,$monEnd,$mdayEnd,$hourEnd,$minEnd,$secEnd)=($endDatetime=~/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{5})$/);
	my $startDateTimeMillSec=$hourStart*3600*1000+$minStart*60*1000+$secStart;
	my $endDateTimeMillSec=$hourEnd*3600*1000+$minEnd*60*1000+$secEnd;
	my $TAT=($endDateTimeMillSec-$startDateTimeMillSec)/1000.0;
	my $TATRounded=sprintf("%.4f",$TAT);
	$TATRounded;
}

close MYFILE_REQ;
close MYFILE_REQ_CON;
close MYFILE_RES;
close OUTPUT;